/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.widget.item.impl;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.form.client.CssNames;
import com.genia.toolbox.web.gwt.form.client.form.CheckBoxItem;
import com.genia.toolbox.web.gwt.form.client.widget.item.ItemWidgetModifyListener;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * <code>SimpleItemWidget</code> representing a checkbox.
 */
public class CheckBoxItemWidget
    extends AbstractSimpleValuedItemWidget<CheckBoxItem>
{
  /**
   * the <code>CheckBox</code> to display.
   */
  private final transient CheckBox checkBoxWidget = new CheckBox();

  /**
   * the widgets that the setErrorMessage must modify.
   */
  private final transient Widget[] widgetsToToggle = new Widget[] { checkBoxWidget };



  /**
   * constructor.
   * 
   * @param item
   *          the item this object represents
   */
  public CheckBoxItemWidget(final CheckBoxItem item)
  {
    super(item);
    // get properties from a CheckBoxItem.
    final GwtI18nMessage labelKey = item.getLabelKey();
    final String name = item.getName();
    final boolean enableState = item.isEnabled();

    // get a CheckBox Widget.
    GwtHelper.fillText(checkBoxWidget, labelKey);

    checkBoxWidget.addStyleName(CssNames.CHECK_BOX_CSS_STYLE);
    checkBoxWidget.addStyleName(item.getName());

    checkBoxWidget.setEnabled(enableState);
    checkBoxWidget.setName(name);

    initWidget(checkBoxWidget);
  }



  /**
   * returns the current value of this item.
   * 
   * @return a <code>String</code> representing the current values of this item
   */
  @Override
  public String getValue()
  {
    return String.valueOf(checkBoxWidget.isChecked());
  }



  /**
   * add a listener that is called each time the value of this object change.
   * 
   * @param modifyListener
   *          the callback
   */
  public void addModifyListener(final ItemWidgetModifyListener modifyListener)
  {
    checkBoxWidget.addClickListener(new ClickListener()
    {
      public void onClick(final Widget widget)
      {
        modifyListener.onModify(CheckBoxItemWidget.this);
      }
    });
  }



  /**
   * set the current value of this item.
   * 
   * @param value
   *          a <code>String</code> representing the new values of this item
   */
  @Override
  public void setValue(final String value)
  {
    checkBoxWidget.setChecked(Boolean.valueOf(value).booleanValue());
  }



  /**
   * returns an array containing all the widgets that the setErrorMessage must
   * modify.
   * 
   * @return an array containing all the widgets that the setErrorMessage must
   *         modify
   */
  @Override
  protected Widget[] getWidgetsToToggle()
  {
    return widgetsToToggle;
  }

}
